using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UIManager : MonoBehaviour
{
    [Header("面板")]
    [SerializeField] private GameObject loginPanel;
    [SerializeField] private GameObject mainMenuPanel;
    [SerializeField] private GameObject matchingPanel;
    [SerializeField] private GameObject gamePanel;
    [SerializeField] private GameObject gameOverPanel;

    [Header("登录界面")]
    [SerializeField] private TMP_InputField usernameInput;
    [SerializeField] private TMP_InputField passwordInput;
    [SerializeField] private Button loginButton;

    [Header("主菜单")]
    [SerializeField] private TextMeshProUGUI userInfoText;
    [SerializeField] private Button matchButton;
    [SerializeField] private Button quitButton;

    [Header("匹配界面")]
    [SerializeField] private TextMeshProUGUI matchingText;
    [SerializeField] private Button cancelMatchButton;

    [Header("游戏界面")]
    [SerializeField] private TextMeshProUGUI gameInfoText;
    [SerializeField] private TextMeshProUGUI turnInfoText;
    [SerializeField] private TextMeshProUGUI opponentText;
    [SerializeField] private Button surrenderButton;
    [SerializeField] private Button backButton;

    [Header("游戏结束")]
    [SerializeField] private TextMeshProUGUI gameResultText;
    [SerializeField] private Button backToMenuButton;

    [Header("消息提示")]
    [SerializeField] private GameObject messagePanel;
    [SerializeField] private TextMeshProUGUI messageText;
    [SerializeField] private float messageDuration = 2f;

    private void Start()
    {
        // 绑定按钮事件
        loginButton.onClick.AddListener(OnLoginClick);
        matchButton.onClick.AddListener(OnMatchClick);
        cancelMatchButton.onClick.AddListener(OnCancelMatchClick);
        surrenderButton.onClick.AddListener(OnSurrenderClick);
        backButton.onClick.AddListener(OnBackClick);
        backToMenuButton.onClick.AddListener(OnBackToMenuClick);
        quitButton.onClick.AddListener(OnQuitClick);

        // 显示登录界面
        ShowLoginPanel();
    }

    private void ShowLoginPanel()
    {
        HideAllPanels();
        loginPanel.SetActive(true);
    }

    public void ShowMainMenu()
    {
        HideAllPanels();
        mainMenuPanel.SetActive(true);
    }

    public void ShowMatchingUI()
    {
        HideAllPanels();
        matchingPanel.SetActive(true);
        matchingText.text = "正在匹配中...";
    }

    public void ShowGameUI()
    {
        HideAllPanels();
        gamePanel.SetActive(true);
    }

    public void ShowGameOverDialog(string result)
    {
        gameOverPanel.SetActive(true);
        gameResultText.text = result;
    }

    private void HideAllPanels()
    {
        loginPanel.SetActive(false);
        mainMenuPanel.SetActive(false);
        matchingPanel.SetActive(false);
        gamePanel.SetActive(false);
        gameOverPanel.SetActive(false);
    }

    public void UpdateUserInfo(string username, int score, int winCount, int loseCount)
    {
        userInfoText.text = $"玩家: {username}\n积分: {score}\n胜: {winCount} 负: {loseCount}";
    }

    public void UpdateGameInfo(string colorInfo, string opponentName)
    {
        gameInfoText.text = colorInfo;
        opponentText.text = $"对手: {opponentName}";
    }

    public void UpdateTurnInfo(string turnInfo)
    {
        turnInfoText.text = turnInfo;
    }

    public void ShowMessage(string message)
    {
        messagePanel.SetActive(true);
        messageText.text = message;
        CancelInvoke(nameof(HideMessage));
        Invoke(nameof(HideMessage), messageDuration);
    }

    private void HideMessage()
    {
        messagePanel.SetActive(false);
    }

    // 按钮事件
    private void OnLoginClick()
    {
        string username = usernameInput.text.Trim();
        string password = passwordInput.text.Trim();

        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
        {
            ShowMessage("请输入用户名和密码");
            return;
        }

        GameManager.Instance.Login(username, password);
    }

    private void OnMatchClick()
    {
        GameManager.Instance.RequestMatch();
    }

    private void OnCancelMatchClick()
    {
        GameManager.Instance.CancelMatch();
    }

    private void OnSurrenderClick()
    {
        GameManager.Instance.Surrender();
    }

    private void OnBackClick()
    {
        GameManager.Instance.BackToMainMenu();
    }

    private void OnBackToMenuClick()
    {
        gameOverPanel.SetActive(false);
        GameManager.Instance.BackToMainMenu();
    }

    private void OnQuitClick()
    {
        Application.Quit();
    }
}
